<?php

namespace App\Admin\Controllers;

use App\Models\PointBag;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class PointBagController extends Controller
{
    use ModelForm;

    protected $header = '福袋管理';
    protected $description = '管理所有福袋';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description($this->description);

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(PointBag::class, function (Grid $grid) {
            $grid->name('名称');
            $grid->description('描述');
            $grid->point('积分');
            $grid->image('图片')->display(function ($image) {
                return '<img style="height:50px;" src="' . asset('storage/' . $image) . '">';
            });
            $grid->growing_coffee('成长咖啡')->display(function ($growing_coffee) {
                return $growing_coffee['min'] . '至' . $growing_coffee['max'];
            });
            $grid->discount_coupon('折扣券')->display(function ($discount_coupon) {
                return $discount_coupon['min'] . '至' . $discount_coupon['max'];
            });
            $grid->free_coupon('免费咖啡券几率')->display(function ($free_coupon) {
                return $free_coupon * 100 . '%';
            });
            $grid->filter(function ($filter) {
                $filter->disableIdFilter();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(PointBag::class, function (Form $form) {
            $form->text('name', '福袋名称');
            $form->text('description', '描述');
            $form->image('image', '图片');
            $form->number('point', '积分');
            $form->number('sort_num', '排序');
            $form->embeds('free_coupon', '免费咖啡券', function ($form) {
                $form->number('percent', '免费咖啡券几率')->rules('required')->max(1);
                $form->number('expired_at', '有效时长（天）')->rules('required');
            });
            $form->embeds('growing_coffee', '成长咖啡范围', function ($form) {
                $form->number('min', '成长咖啡下限')->rules('required')->max(1);
                $form->number('max', '成长咖啡上限')->rules('required')->max(1);
                $form->number('expired_at', '有效时长（天）')->rules('required');
            });

            $form->embeds('discount_coupon', '折扣券范围', function ($form) {
                $form->number('min', '折扣券范围下限')->rules('required')->max(1);
                $form->number('max', '折扣券范围上限')->rules('required')->max(1);
                $form->number('expired_at', '有效时长（天）')->rules('required');
            });

        });
    }
}
